package com.kevin.minio.config;

import com.google.common.base.Throwables;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import lombok.Builder;
import lombok.Data;
import lombok.val;
import org.apache.commons.lang3.StringUtils;

/**
 * @author Kevin Liu
 * @date 2020/6/9 7:53 下午
 */
@Data
@Builder
public class MinioConfiguration {

    private String clientUrl;

    private String username;

    private String password;

    public MinioConfiguration(String clientUrl, String username, String password) {
        this.clientUrl = clientUrl;
        this.username = username;
        this.password = password;
    }

    public MinioClient openClient() {
        if (StringUtils.isEmpty(clientUrl)) {
            throw new RuntimeException("连接地址不能为空");
        }
        if (StringUtils.isEmpty(username)) {
            throw new RuntimeException("连接用户名不能为空");
        }
        if (StringUtils.isEmpty(password)) {
            throw new RuntimeException("连接密码不能为空");
        }
        try {
            return new MinioClient(clientUrl, username, password);
        } catch (InvalidEndpointException | InvalidPortException e) {
            throw new RuntimeException(Throwables.getStackTraceAsString(e));
        }
    }

}
